<?php
/**
 * @name /lib/public/HTMLControls/Combobox.php
 * @author Michael Foss <webdesign@matatechconsulting.com>
 * @version 2008.09.06
 *
 * Combobox class library.
 */

ReqLib('/lib/public/HTMLControls/HTMLControl.php');

/**
 * Combobox class.
 *
 */
class Combobox extends HTMLControl {
  var $Items;
  var $Label;
  var $SelectedIndex;
  var $OnChange;
  var $Disabled;
  var $ContainerStyle;
  function Combobox($Id = '', $Label = '') {
    $this->HTMLControl($Id);
    $this->Items = array();
    $this->Label = $Label;
    $this->SelectedIndex = -1;
    $this->Disabled = false;
    $this->ContainerStyle = '';
  }
  function AddItem($Value, $Text) {
    $this->Items[] = array($Value, $Text);
    return true;
  }
  function DeleteItem($Value, $Text) {
    $location = array_search(array($Value, $Text), $this->Items);
    if ($location === false)
      return false;
    // Create a new list without the current item
    $newItems = array();
    for ($i = 0; $i < $location; $i++)
      $newItems[] = $this->Items[$i];
    for ($i = $location; $i < count($this->Items); $i++)
      $newItems[] = $this->Items[$i];
    $this->Items = $newItems;
    // Shift the selected index if necessary (unless it's the first item)
    if ($this->SelectedIndex >= $location && $location != 0)
      $this->SelectedIndex--;
    return true;
  }
  function SetSelectedIndex($SelectedIndex = -1) {
    $this->SelectedIndex = $SelectedIndex;
  }
  function GetSelectedIndex() {
    return $this->SelectedIndex;
  }
  function GetSelectedValue() {
    if (count($this->Items) > $this->SelectedIndex)
      return $this->Items[$this->SelectedIndex][0];
    else
      return false;
  }
  function GetSelectedText() {
    if (count($this->Items) > $this->SelectedIndex)
      return $this->Items[$this->SelectedIndex][1];
    else
      return false;
  }
  function Disable() {
    $this->Disabled = true;
  }
  function Enable() {
    $this->Disabled = false;
  }
  function SetLabel($Label = '') {
    $this->Label = $Label;
  }
  function GetLabel() {
    return $this->Label;
  }
  function Display() {
    if (!$this->Id)
      return false;
    if ($this->SelectedIndex != -1 && $this->SelectedIndex < count($this->Items))
      $SelectedIndex = $this->SelectedIndex;
    else
      $SelectedIndex = -1;
    if ($this->ContainerStyle)
      print('<div class="ComboboxContainer" style="' . htmlspecialchars($this->ContainerStyle) . '">');
    else
      print('<div class="ComboboxContainer">');
    if ($this->Label) {
      print('<div>');
      print('<label for="' . htmlspecialchars($this->Id) . '">' . htmlspecialchars($this->Label) . '</label>');
      print('</div>');
    }
    print('<div>');
    print('<select name="' . htmlspecialchars($this->Id) . '" id="' . htmlspecialchars($this->Id) . '"' . ($this->OnChange ? ' onchange="' . str_replace('"', '\"', $this->OnChange) . '"' : '') . ($this->Disabled ? ' disabled="disabled"' : '') . '>');
    for ($i = 0; $i < count($this->Items); $i++) {
      $Item = $this->Items[$i];
      print('<option value="' . htmlspecialchars($Item[0]) . '"' . ($SelectedIndex == $i ? ' selected="selected">' : '>') . htmlspecialchars($Item[1]) . '</option>');
    }
    print('</select>');
    print('</div>');
    print('</div>');
    return true;
  }
}